package libnet

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"net"
)

func NetDail() {
	conn, err := net.Dial("tcp", "www.baidu.com:80")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
	fmt.Println("返回本地网络地址",conn.RemoteAddr())
	fmt.Println("返回远端网络地址",conn.LocalAddr())
	r := bufio.NewReader(conn)
	chunks := make([]byte, 1024, 1024)
	buf := make([]byte, 1024)
	for {
		n, err := r.Read(buf)
		if err != nil && err != io.EOF {
			log.Fatal(err)
			panic(err)
		}
		if n == 0 {
			break
		}
		fmt.Println(buf[:n])
		chunks = append(chunks, buf[:n]...)
	}
	fmt.Println(string(chunks))
	fmt.Println(" 结束")
}
